package org.alex.samples.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.alex.samples.model.ExamUserRole;
import org.alex.samples.model.MD5Utils;
import org.alex.samples.model.User;
import org.alex.samples.service.IExamUserRoleService;
import org.alex.samples.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class LoginController {
	
	@Autowired
	private IUserService userService;
	
	@Autowired 
	private IExamUserRoleService roleService ;
	
	//进入登录界面
	@RequestMapping(value = "/login",method = RequestMethod.GET)
	public String toLogin(Model model){
		model.addAttribute("user", new User());
		return "login/login";
	}
	//进行登录
	//value object
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public String deLogin(RedirectAttributes redirectAttributes,User vo,HttpSession session){
		 
		if(vo.getId() !=null && vo.getPsw() !=null){
			 User resultVO = userService.getUserById(vo.getId());
	         try{
	        	if(resultVO !=null){
		        String encodePassword = MD5Utils.GetMD5Code(vo.getPsw());
			    //密码正确的情况
			    Date currentTime = null;
			    Calendar currentTimecalendar = Calendar.getInstance();//创建日历对象
			    currentTimecalendar.setTime(new Date());  //初始化时间
			    currentTimecalendar.add(Calendar.MINUTE, -5);  //减5分
			    currentTime = currentTimecalendar.getTime();
			    System.out.println(currentTime + "---------------" + new Date());
			        if(resultVO.getLastLockTime().before(currentTime)){
				    //不在锁定时间范围内
				    System.out.println("不在锁定时间范围内");
				        if(encodePassword.equals(resultVO.getPsw())){
					    //登录成功时清
					    resultVO.setLoginFailCount(0);
					    userService.updateUserLoginFailCount(resultVO);
					    session.setAttribute("User", resultVO);
					    redirectAttributes.addFlashAttribute("message", "登录成功.");
					   // redirectAttributes.addFlashAttribute("user", serviceimpl.getExamUserRole(userID));
//					    public ExamUserRole getExamUserRole(String userID){
//					    	for(ExamUserRole e : this.createQuery("FROM ExamUserRole WHERE userId="+userID).list()){
//					    		return e;
//					    	}
//					    }
					   // this.createQery("FROM ExamUserRole WHERE userId="+studentID).list();
					    List<ExamUserRole> us = roleService.findAllRole();
					    for(ExamUserRole r : us){
					    	if(r.getUserId().equals(resultVO.getId())){
					    		//redirectAttributes.addFlashAttribute("user", r);
					    		session.setAttribute("user1", r);
					    	}
					    }
					    
					    
					    return "redirect:/index";
				        }else{
				        redirectAttributes.addFlashAttribute("message", "用户名存在，密码输入错误");
					    resultVO.setLoginFailCount(resultVO.getLoginFailCount() + 1);
					        if(resultVO.getLoginFailCount() == 3){
						    resultVO.setLoginFailCount(0);
						    resultVO.setLastLockTime(new Date());
						    userService.updateUserLoginFailCount(resultVO);
					       }
					       userService.updateUserLoginFailCount(resultVO);
				        }
			        }else {
				    //锁定时间范围内
				    System.out.println("目前账户被锁定，无法登录，请稍等5分钟。");
				    redirectAttributes.addFlashAttribute("message", "目前账户被锁定，无法登录，请等待。。。");
			       }
	        	}else{
	        		redirectAttributes.addFlashAttribute("message", "用户名不存在");
	        	}
		    }catch(Exception e){
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "登录失败！");
		    }
		}
	   return "redirect:/login";
       } 
	
	//退出
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:/login";
	}
}
